/*structure.cpp*/
#include "structure.h"

structure::structure() {
	snode.push_back(node(0, 1000, 1));
	snode.push_back(node(1000, 1000, 2));
	snode.push_back(node(0, 0, 3));
	snode.push_back(node(1000, 0, 4));
	snode.push_back(node(2000, 0, 5));
	snode.push_back(node(500, 1000, 6));
	snode.push_back(node(0, 500, 7));
	snode.push_back(node(500, 500, 8));
	snode.push_back(node(1000, 500, 9));
	snode.push_back(node(1500, 500, 10));
	snode.push_back(node(500, 0, 11));
	snode.push_back(node(1500, 0, 12));
	snode.push_back(node(250, 750, 13));
	snode.push_back(node(750, 750, 14));
	snode.push_back(node(1250, 750, 15));
	snode.push_back(node(250, 250, 16));
	snode.push_back(node(750, 250, 17));
	snode.push_back(node(1250, 250, 18));
	snode.push_back(node(1750, 250, 19));
	node_num = 19;
	sunit.push_back(unit(snode[0], snode[6], snode[12]));
	sunit.push_back(unit(snode[6], snode[2], snode[15]));
	sunit.push_back(unit(snode[0], snode[12], snode[5]));
	sunit.push_back(unit(snode[6], snode[15], snode[12]));
	sunit.push_back(unit(snode[7], snode[12], snode[15]));
	sunit.push_back(unit(snode[2], snode[10], snode[15]));
	sunit.push_back(unit(snode[5], snode[12], snode[13]));
	sunit.push_back(unit(snode[12], snode[7], snode[13]));

	sunit.push_back(unit(snode[7], snode[15], snode[16]));
	sunit.push_back(unit(snode[15], snode[10], snode[16]));
	sunit.push_back(unit(snode[5], snode[13], snode[1]));
	sunit.push_back(unit(snode[7], snode[16], snode[13]));
	sunit.push_back(unit(snode[8], snode[13], snode[16]));
	sunit.push_back(unit(snode[10], snode[3], snode[16]));
	sunit.push_back(unit(snode[1], snode[13], snode[8]));
	sunit.push_back(unit(snode[8], snode[16], snode[17]));

	sunit.push_back(unit(snode[1], snode[8], snode[14]));
	sunit.push_back(unit(snode[16], snode[3], snode[17]));
	sunit.push_back(unit(snode[8], snode[9], snode[14]));
	sunit.push_back(unit(snode[9], snode[8], snode[17]));
	sunit.push_back(unit(snode[3], snode[11], snode[17]));
	sunit.push_back(unit(snode[9], snode[17], snode[11]));

	sunit.push_back(unit(snode[9], snode[11], snode[18]));
	sunit.push_back(unit(snode[11], snode[4], snode[18]));
	unit_num = 24;
}
bool structure::find_node(int u_in, int n_in) {
	if (sunit[u_in].find_index(n_in) == -1) return true;
	else return false;
}
double structure::Ksum(int f1, int f2, double E, double nu, double t) {
	double K = 0;
	int index1 = (f1 + 1) / 2, index2 = (f2 + 1) / 2;
	int fnode1 = (f1 + 1) % 2+1, fnode2 = (f2 + 1) % 2+1;
	for (int ui = 0; ui < unit_num; ui++) {
		if (find_node(ui, index1) || find_node(ui, index2)) continue;
		K += sunit[ui].K_node(index1, fnode1, index2, fnode2, E, nu, t);
	}
	return K;
}
Matrix structure::unit_B(int index) {
	Matrix bu(3, 6);
	for (int c = 1; c < 4; c++)for (int i = 0; i < 3; i++)for (int f = 1; f < 3; f++) {
		bu[c - 1][i * 2 + f - 1] = sunit[index].B_node(i, f, c);
	}
	return bu;
}
Matrix structure::unit_S(int index, double E, double nu) {
	Matrix su(3, 6);
	for (int c = 1; c < 4; c++)for (int i = 0; i < 3; i++)for (int f = 1; f < 3; f++) {
		su[c - 1][i * 2 + f - 1] = sunit[index].S_node(i, f, c, E, nu);
	}
	return su;
}
Matrix structure::unit_d(int index, vector<double> delta_s) {
	Matrix du(6, 1);
	for (int i = 0; i < 3; i++) {
		du[i * 2][0] = delta_s[sunit[index][i] * 2 - 2];
		du[i * 2 + 1][0] = delta_s[sunit[index][i] * 2 - 1];
	}
	return du;
}
Matrix structure::d_xy(double x, double y, vector<double> delta_s) {
	Matrix dxy(2, 1);
	Matrix ds(6, 1);
	double n0 = 0, n1 = 0;
	for (int i = 0; i < unit_num; i++) {
		n0 = sunit[i].N_point(0, x, y);
		n1 = sunit[i].N_point(1, x, y);
		if (n0 + n1 > 1 || n0 + n1 < 0 || n0 * n1 < 0) continue;
		else {
			ds = unit_d(i, delta_s);
			for (int j = 0; j < 2; j++) dxy[j][0] = n0 * ds[0 + j][0] + n1 * ds[2 + j][0] + (1 - n0 - n1) * ds[4 + j][0];
		}
	}
	return dxy;
}